<?php
/*
 * @Yourtion 
 * http://www.yourtion.com
 * @copyright 2011
 */
session_start();
// include_once ('config.php');
include_once ('tencentWeibo.php');

if (!empty($_GET['oauth_verifier']) && !empty($_SESSION['keys']['oauth_token']) &&
    !empty($_SESSION['keys']['oauth_token']))
{
    // SinaOAuth 对象实例，注意新加入的两个参数
    $sinaOAuth = new WeiboOAuth(WB_AKEY, WB_SKEY, $_SESSION['keys']['oauth_token'],$_SESSION['keys']['oauth_token_secret']);
    // 获取 access token
    $access_token = $sinaOAuth->getAccessToken($_REQUEST['oauth_verifier']);
    print_r($access_token); exit;
    // 将获取到的 access token 保存到 Session 中
    $_SESSION['access_token'] = $access_token;
    // 获取用户信息
    $user_info = $sinaOAuth->get('account/verify_credentials');
    // 打印用户信息
print_r($user_info);
exit;

    mysql_connect(DATABASE_HOST, DATABASE_USER, DATABASE_PSSWORD);
    mysql_select_db(DATABASE_DB_NAME);
    if (isset($user_info->error) or empty($user_info['id']))
    {
        // Something's wrong, go back to square 1
        header('Location: index.php');
    } else
    {
        // Let's find the user by its ID
        $sql = "SELECT * FROM oauth_users WHERE oauth_provider='sina' AND oauth_uid=" .$user_info['id'];
        $query = mysql_query($sql);
        $result = mysql_fetch_array($query);
        // If not, let's add it to the database
        if (empty($result))
        {
            $sql = "INSERT INTO oauth_users (oauth_provider, oauth_uid, username, oauth_token, oauth_secret) VALUES ('sina', '" .
                $user_info['id'] . "', '" . $user_info['screen_name'] . "', '" . $access_token['oauth_token'] .
                "', '" . $access_token['oauth_token_secret'] . "')";
            $query = mysql_query($sql);
            $query = mysql_query("SELECT * FROM oauth_users WHERE id = ".mysql_insert_id());
            $result = mysql_fetch_array($query);
        } else
        {
            // Update the tokens
            $query = mysql_query("UPDATE oauth_users SET oauth_token = '" . $access_token['oauth_token'] .
                "', oauth_secret = '" . $access_token['oauth_token_secret'] .
                "' WHERE oauth_provider = 'sina' AND oauth_uid = " . $user_info['id']);
        }
        $_SESSION['id']=$result['id'];
        $_SESSION['username']=$result['username'];
        $_SESSION['oauth_uid']=$result['oauth_uid'];
        $_SESSION['oauth_provider']=$result['oauth_provider'];
        $_SESSION['oauth_token']=$result['oauth_token'];
        $_SESSION['oauth_secret']=$result['oauth_secret'];
		header('Location: update.php');
    }
} else
{
    // 数据不完整，转到上一步
    header('Location: index.php');
}

?>